import { NzMessageService } from 'ng-zorro-antd/message';
import { ActivatedRoute } from '@angular/router';
import { DeviceServiceSvr } from './../../service/device.service';
import { Component, OnInit } from '@angular/core';
import { DeviceList } from 'src/app/domains/device.domain';
import { RepairApplyServiceSvr } from 'src/app/service/repairApply.service';

@Component({
  selector: 'app-rent-management-repair',
  templateUrl: './rent-management-repair.component.html',
  styleUrls: ['./rent-management-repair.component.less']
})
export class RentManagementRepairComponent implements OnInit {

  constructor( private deviceServiceSvr: DeviceServiceSvr,
    private activatedRoute: ActivatedRoute,
    private repairApplyServiceSvr: RepairApplyServiceSvr,
    private message: NzMessageService
  ){}

  token: string = JSON.parse(localStorage.getItem('Token')!)
  customerId: string = JSON.parse(localStorage.getItem('customerId')!)
  current = 1;
  houseId: string = ''
  isOrNotRepairList: any
  repairFlag: boolean = false
  detailModel: boolean = false
  deviceRepairForm: any
  // 维修记录列表
  repairRecordList: any
  // 条件维修记录列表
  repairListFilter: any
  // 撤回弹窗
  recallModel: boolean = false
  // 报修弹窗
  repairModel: boolean = false
  // 设备状态
  deviceState: number = 0
  deviceId: string = ''
  description: string = ''
  // 设备列表
  deviceList: any




  ngOnInit(): void {
    this.getAllDevice()
  }


  // 获取房屋ID
  getHouseId() {
    this.activatedRoute.queryParams.subscribe(queryParams => {
      this.houseId = JSON.parse(queryParams['houseData']).Id
    })
  }

  // 获取所有的维修列表
  async getAllDevice() {
    this.getHouseId()
    let result = await this.repairApplyServiceSvr.listRepairApplications(this.token, 0, 0, '', '', 1, -1)
    this.repairRecordList = result._Items
    this.repairListFilter = result._Items
  }

  //获取过滤后的维修列表
  getRepairListFilter(state: number) {
    this.repairListFilter = this.repairRecordList.filter((item: { State: number; }) => {
      if(item.State === state) return item;
      return;
    })
  }


  // 撤销报修
  async cancelRepairApply(deviceId: string) {
    this.recallModel = true
    this.deviceId = deviceId
  }
  async handleOk() {
    try {
      let result = await this.repairApplyServiceSvr.updateIsWithdraw(this.token, this.deviceId)
      this.recallModel = false
      if(result===1) {
        this.message.success('报修申请撤回成功')
      } else {
        this.message.error('报修申请撤回失败')
      }
    } catch (error) {
      console.log(error)
    }
    this.getAllDevice()
  }
  handleCancel() {
    this.recallModel = false
  }


  // 报修设备
  async repairApply() {
    this.repairModel = true
  }
  async handleOkRepair() {
    try {
      let result = await this.repairApplyServiceSvr.saveRepairApply(this.token, this.deviceId, 1, this.customerId, this.description)
      this.message.success('设备报修成功')
      this.repairModel = false
    } catch (error) {
      this.message.error('设备报修失败')
      console.log(error)
    }
    this.getAllDevice()

  }
  handleCancelRepair() {
    this.repairModel = false
  }
  async handleOpen() {
    let result = await this.deviceServiceSvr.listDevices(this.token, this.houseId, 0, 0, 100)
    this.deviceList = result._Items
  }


   // 格式化时间
   formatTime(time: string | number | Date) {
    time = new Date(time)
    let year = time.getFullYear()
    let month = time.getMonth() + 1
    let date = time.getDate()
    return year+'/'+month+'/'+date
  }

  // 重置输入框
  close() {
    this.deviceState = 0
    this.deviceId = ''
    this.description = ''
  }

}
